from tkinter import Tk, ttk, Text
from tkinter.constants import LEFT, RIGHT, BOTTOM, TOP, BOTH, X, Y, WORD, NSEW, END
from tkinter.ttk import Notebook

import ttkbootstrap
from tkhtmlview import HTMLLabel, HTMLText
from ttkbootstrap import Window, ScrolledText, Button, Entry
import ttkbootstrap  as tb


def markdown2html(md_content):
    import markdown2
    import markdown
    html = markdown.markdown(md_content, extensions=['fenced_code', 'codehilite'])
    # html = markdown2.markdown(md_content, extras=["fenced-code-blocks", "cuddled-lists"])
    return html

def htmllabel(msg_frame):
    html = '<h1 style="color: red; text-align: center"> Hello World </H1>'
    html_label = HTMLLabel(msg_frame, html=html)
    html_label.pack(fill="both", expand=True)

    html_label.fit_height()

    message = """
# alembic数据库迁移增删改查

## 运行时加载.env环境变量配置
修改 alembic/env.py

在代码前面加上环境变量语句
```
# 加载 .env 文件中的变量
from dotenv import load_dotenv
load_dotenv()
```

## 根据模型model类生成数据表
```
alembic revision --autogenerate -m "initial migration"
```

## 更新版本信息
model配置修改之后,执行以下命令生成新的alembic/versions 版本文件

```
alembic revision --autogenerate -m "modify users add nickname"
```

如果是新增模型类,那么需要在 alembic/env.py 中引入模型类(只需要引入即可)

```py

from app.models.ai import AIApiKey

```
        """
    html = markdown2html(message)
    # , font = ("Microsoft YaHei", 4, "italic")
    html_label = HTMLText(msg_frame, html=html, font = ("Microsoft YaHei", 4, "italic"))
    html_label.pack(fill="both", expand=True)

    html_label.fit_height()

    message = f"""{message}\n## ---更新最新版本到数据库
```
alembic upgrade head
```

    """
    html = markdown2html(message)
    # html_label.insert(END, html)
    # html_label.see(END)
    html = f"""
        {html}
        """

    from tkhtmlview.html_parser import Defs

    Defs.FONT_SIZE = 4
    Defs.HEADINGS_FONT_SIZE = {
        "h1": 6,
        "h2": 5,
        "h3": 4,
        "h4": 3,
        "h5": 2,
        "h6": 1,
    }
    #<p> 的字体大小无法设置,只能通过替换的方式设置
    html=html.replace('<p>','<p style="font-size:10px;">')#有效果
    html_label.set_html(html,strip=False)
    html_label.setvar(name="font", value=("Microsoft YaHei", 4, "italic"))
    print(html)


if __name__ == '__main__':
    root = Tk()
    root.title("AI智能助手")
    root.geometry("1000x900")

    # 创建文本编辑框并设置字体为微软雅黑
    text_box = Text(root, font=("Microsoft YaHei", 44, "italic"), wrap=WORD, height=2)
    text_box.pack(side=TOP, fill=X, expand=False)  # 确保text_box在上方，高度为10，填充X方向
    text_box.insert(END, '你好世界')

    msg_frame = ttkbootstrap.Frame(root, bootstyle="info")
    msg_frame.pack(side=BOTTOM, fill=BOTH, expand=True)  # 确保HTMLLabel在下方，填充剩余空间

    htmllabel(msg_frame)

    root.mainloop()
